#!/usr/bin/env python

import wx
import wx.lib.agw.balloontip

import louise.single.viewer

from .._base import Base
from resources import icons
from resources.strings.single import viewer as r

class Viewer(Base):
    
    _lineId = wx.NewId()
    _data = None
    _step = 5
    _y = None
    
    def __init__(self, view, interactor):
        super(Viewer, self).__init__(view, interactor)
        config = louise.single.viewer.Config()
        self.restoreSize(config)
        self.restorePosition(config)
        view.Icon = icons.Single.Icon
        view.Title = r.description
        
    def onClose(self):
        config = louise.single.viewer.Config()
        self.savePosition(config)
        self.saveSize(config)
    
    def open(self):
        path = self.view.fileDialog(wildcard='*.acl')
        if not path: return
        
        self.draw(path)
        
    def draw(self, path):
        self.view.busyInfo('Drawing...')
        self._step = 5
        
        f = open(path, 'r')
        self._data = [line.strip() for line in f.readlines()]
        f.close()
        
        canvas = self.view.canvas
        pdc = canvas.pdc
        pdc.RemoveAll()
        
        canvas.Scroll(0, 0)
        canvas.maxHeight = self._step * len(self._data)
        canvas.maxWidth = 1024
        canvas.SetVirtualSize((canvas.maxWidth, canvas.maxHeight))
        canvas.pdc.BeginDrawing()
        
        previousData = None
        y = 0
        for line in self._data:
            data = line.split('\t')
            if previousData:
                x1 = int(previousData[1])
                y1 = y
                y += self._step
                x2 = int(data[1])
                y2 = y
                pdc.DrawLine(x1, y1, x2, y2)
            previousData = data
        
        pdc.SetId(self._lineId)
        pdc.RemoveId(self._lineId)
        pdc.DrawLine(0, 0, 1024, 0)
        r = wx.Rect(0, 0, 1024, 0)
        r.Inflate(4, 4)
        pdc.SetIdBounds(self._lineId, r)
        
        canvas.pdc.EndDrawing()
        canvas.Refresh()
        
        self.view.idle()
    
    def offsetRect(self, r):
        canvas = self.view.canvas
        xView, yView = canvas.GetViewStart()
        xDelta, yDelta = canvas.GetScrollPixelsPerUnit()
        r.OffsetXY(-(xView * xDelta), -(yView * yDelta))
        
    def convertEventCoords(self, x, y):
        canvas = self.view.canvas
        xView, yView = canvas.GetViewStart()
        xDelta, yDelta = canvas.GetScrollPixelsPerUnit()
        return (x + (xView * xDelta), y + (yView * yDelta))

    def move(self, position):
        pdc = self.view.canvas.pdc
        r = pdc.GetIdBounds(self._lineId)
        x, y = self.convertEventCoords(position[0], position[1])
        dx = x - r.x - (r.width / 2)
        dy = y - r.y - (r.height / 2)
        pdc.TranslateId(self._lineId, 0, dy)
        r2 = pdc.GetIdBounds(self._lineId)
        r = r.Union(r2)
        r.Inflate(4, 4)
        self.offsetRect(r)
        self.view.canvas.RefreshRect(r, False)
        y = int(y / self._step)
        if y != self._y:
            try:
                self.view.status = self._data[y]
                self._y = y
            except:
                self.view.status = ''